#include"Stack.h"
void StackInit(Stack* pst)
{
	assert(pst);
	pst->a=(STDataType*) malloc(sizeof(STDataType) * 4);
	pst->top = -1;
	pst->capacity = 4;
}

void StackPush(Stack* pst, STDataType x)
{
	assert(pst);
	if (pst->top == pst->capacity)
	{
		STDataType* newStack =(STDataType*) realloc(pst->a,sizeof(STDataType)* pst->capacity* 2);
		if (newStack == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			pst->capacity*=2;
		}
	}
	pst->a[pst->top+1] = x;
	pst->top++;
}

void StackPrint(Stack* pst)
{
	assert(pst->a);
	for(int i=0;i<=pst->top;i++)
	{
		printf("%d\t", pst->a[i]);
	}
	//printf("\n");
}

void StackPop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	pst->top--;
}

BOOL StackEmpty(Stack* pst)
{
	assert(pst);
	return pst->top == -1;
}

BOOL StackFull(Stack* pst)
{
	assert(pst);
	return pst->top+1 == pst->capacity;
}

STDataType StackTop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	return pst->a[pst->top];
}

void StackDestroy(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	free(pst->a);
	pst->a = NULL;
	pst->capacity = pst->top= 0;
}

int StackSize(Stack* pst)
{
	return pst->top + 1;
}